package chamster;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import chamster.actions.ForewardAction;
import chamster.actions.HamsterAction;
import chamster.actions.GoFrontLeftAction;
import chamster.actions.GoFrontRightAction;
import chamster.actions.MoveLeftAction;
import chamster.actions.MoveRightAction;
import chamster.random.RandomStatelessActionMachine;
import chamster.random.StaticRandomMachine;
/**
 * Hamster der nach der Amöben Strategie vorgeht
 * @author mark
 *
 */
public class AmoebaHamster extends HistoryHamster {

	private final RandomStatelessActionMachine<AmoebaHamster> machine;
	private final Set<HamsterAction<AmoebaHamster>> oneTimeActions;
	
	public AmoebaHamster() {
		super(3);
		HashMap<HamsterAction<AmoebaHamster>, Integer> actions = 
				new HashMap<HamsterAction<AmoebaHamster>, Integer>();
		actions.put(new ForewardAction<AmoebaHamster>(), 10);
		actions.put(new MoveLeftAction<AmoebaHamster>(), 10);
		actions.put(new MoveRightAction<AmoebaHamster>(), 10);
		actions.put(new GoFrontLeftAction<AmoebaHamster>(), 10);
		actions.put(new GoFrontRightAction<AmoebaHamster>(), 10);
		TurnBackAction<AmoebaHamster> turnBackAction = 
				new TurnBackAction<AmoebaHamster>();
		actions.put(turnBackAction, 10);
		this.oneTimeActions = new HashSet<HamsterAction<AmoebaHamster>>();
		this.oneTimeActions.add(turnBackAction);
		this.machine = new StaticRandomMachine<AmoebaHamster>(actions);
	}
	
	@Override
	public void life() throws Exception {
		HamsterAction<AmoebaHamster> action = this.machine.decideAndExecute(this);
		while(super.chargeLeft(1) && action != null) {
			action.run(this);
			while(action.isApplicable(this) 
					&& !this.oneTimeActions.contains(action)) {
				if(super.canPickup()) {
					super.pickup();
				}
				else if(super.visitedBefore()) {
					action = this.machine.decide(this);
				}
				action.run(this);
			}
			action = this.machine.decide(this);
		}
		System.out.println("accu leer");
	}

}
